package com.wanpinghui.supplier.user.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.user.bean.BusinessTypeBean;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;

import java.util.List;

/**
 * 功能描述
 * author:caitiangui
 * time:2017/11/23
 */

public class BusinessTypeAdapter extends BaseRecyclerViewAdapter<BusinessTypeBean, BusinessTypeAdapter.BusinessTypeViewHolder> {


    public BusinessTypeAdapter(Context context) {
        super(context);
    }

    @Override
    public BusinessTypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_mine_business_type, parent, false);
        //    view.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return new BusinessTypeViewHolder(view);
    }

    public List<BusinessTypeBean> getSelectItem() {
//        List<BusinessTypeBean> beanList = new ArrayList<>();
//        for (BusinessTypeBean bean: datas){
//            if (bean.isSelect()){
//                beanList.add(bean);
//            }
//        }
        return datas;
    }

    @Override
    public void onBindViewHolder(final BusinessTypeViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        final BusinessTypeBean bean = datas.get(position);
        holder.tv_content.setText(bean.getName());
        final TextPaint paint = holder.tv_content.getPaint();
        if (bean.isSelect()) {
            paint.setFakeBoldText(true);
            holder.tv_content.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryOne));
            holder.checkBox.setSelected(true);
        } else {
            paint.setFakeBoldText(false);
            holder.tv_content.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryHint));
            holder.checkBox.setSelected(false);
        }
//        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                bean.setSelect(isChecked);
//                if (isChecked) {
//                    paint.setFakeBoldText(true);
//                    holder.tv_content.setTextColor(ContextCompat.getColor(context,R.color.colorTextPrimaryOne));
//                }else {
//                    paint.setFakeBoldText(false);
//                    holder.tv_content.setTextColor(ContextCompat.getColor(context,R.color.colorTextPrimaryHint));
//                }
//            }
//        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.setSelect(!holder.checkBox.isSelected());
                if (holder.checkBox.isSelected()) {
                    paint.setFakeBoldText(false);
                    holder.tv_content.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryOne));
                } else {
                    paint.setFakeBoldText(true);
                    holder.tv_content.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimaryHint));
                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    class BusinessTypeViewHolder extends ViewHolder {

        TextView tv_content;
        ImageView checkBox;

        BusinessTypeViewHolder(View itemView) {
            super(itemView);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            checkBox = (ImageView) itemView.findViewById(R.id.checkBox);
        }
    }

    public interface setOnItemlistener {
        void onClick(BusinessTypeBean bean);
    }

}
